package com.zzyl.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.entity.NursingPlan;

import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;

import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;

import com.zzyl.vo.NursingPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 2.1 查询所有护理计划
     * @return
     */
    @Override
    public List<NursingPlanVo> getAll() {
        List<NursingPlanVo> list = nursingPlanMapper.getAll();
        return list;
    }

    /**
     * 条件分页查询
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> search(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> page = nursingPlanMapper.search(name, status);
        return PageResponse.of(page, NursingPlanVo.class);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(NursingPlanVo nursingPlanVo) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanVo, NursingPlan.class);
        nursingPlan.setStatus(SuperConstant.STATE_0);
        nursingPlanMapper.addNursingPlan(nursingPlan);
        List<NursingProjectPlan> list = new ArrayList<>();
        nursingPlan.getProjectPlans().forEach(nursingProjectVo -> {
            NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(nursingProjectVo, NursingProjectPlan.class);
            nursingProjectPlan.setPlanId(nursingPlan.getId());
            list.add(nursingProjectPlan);
        });
        nursingProjectPlanMapper.batchAddNursingProjectPlan(list);
    }

    /**
     * 根据id查询护理计划
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getNursingPlanById(Long id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getNursingPlanById(id);
        return nursingPlanVo;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NursingPlanVo nursingPlanVo) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanVo, NursingPlan.class);
        ArrayList<NursingProjectPlan> list = new ArrayList<>();
        nursingPlanVo.getProjectPlans().forEach(nursingProjectPlanVo -> {
            list.add(BeanUtil.toBean(nursingProjectPlanVo, NursingProjectPlan.class));
        });

        nursingPlanMapper.update(nursingPlan);
        //删除老数据中的所有绑定
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(nursingPlan.getId());
        //批量插入前端传来的护理计划项目数据
        nursingProjectPlanMapper.batchAddNursingProjectPlan(list);
    }

    /**
     * 启用禁用
     *
     * @param id
     * @param status
     */
    @Override
    public void status(Long id, Integer status) {
        nursingPlanMapper.status(id, status);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteNursingProjectPlanById(Long id) {
        //启用状态下不能删除
        NursingPlanVo nursingPlanById = nursingPlanMapper.getNursingPlanById(id);
        if (SuperConstant.STATE_1.equals(nursingPlanById.getStatus())) {
            throw new RuntimeException("启用状态下不能删除");
        }
        //如果删除护理计划，则删除护理计划项目关联表中的数据
        nursingPlanMapper.deleteNursingProjectPlanById(id);
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(id);
    }
}
